package com.xiaodeng.xdblogspringboot.controller;


import com.xiaodeng.xdblogspringboot.interceptors.LoginInterceptor;
import com.xiaodeng.xdblogspringboot.pojo.Result;
import com.xiaodeng.xdblogspringboot.tools.AliOssUtil;
import com.xiaodeng.xdblogspringboot.utils.MinioUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;



@Slf4j
@RestController
public class FileUploadController {
    @Autowired
    private MinioUtil minioUtil;

    /*    //阿里云上传文件
    @PostMapping("/uploadByAliYun")
    public Result upload(MultipartFile file) throws IOException {
        //把文件的内容存储到本地磁盘上
        String originalFilename = file.getOriginalFilename();
        //保证文件名是唯一的
        String filename = UUID.randomUUID() +originalFilename.substring(originalFilename.lastIndexOf("."));
        String url = AliOssUtil.uploadFile(filename, file.getInputStream());
        return Result.success("上传成功",url);
    }*/

    /**
     * MinIO上传文件
     *
     * @param file 文件
     * @return 结果
     * @throws Exception 例外
     */


    @PostMapping("/upload")
    public Result upload1(MultipartFile file) throws Exception {
        if (file.isEmpty() || file.getSize() == 0){
            return Result.error("不允许上传空文件");
        }
        String url = minioUtil.upload(file);
        if (null != url) {
            return Result.success("上传成功",url);
        }
        return Result.error("上传失败");
    }

}
